//
//  SceneSetDetailedViewController.m
//  GBKHOME
//
//  Created by XMYY-21 on 2017/9/26.
//
//

#import "SceneSetDetailedViewController.h"
#import "NewEquipment.h"

@interface SceneSetDetailedViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (retain, nonatomic) IBOutlet UITableView *SceneSetTableView;
@property (nonatomic ,strong) NSMutableArray *selectedArr;//选择列表
@property (retain, nonatomic) IBOutlet UITextField *secenNameTextField;

@end

@implementation SceneSetDetailedViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.secenNameTextField.text = self.sceneName;
    
    [self setDataArr];
    
}

-(void)setDataArr{
    self.selectedArr = [NSMutableArray array];

    for (int i = 0; i<self.deviceArr.count; i++) {
        [self.selectedArr addObject:@"1"];
    }

    [self.SceneSetTableView reloadData];
}

#pragma mark uitableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.deviceArr.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SceneSetCell" forIndexPath:indexPath];
    
    UILabel *label = [cell viewWithTag:1];//deviceNameLabel
//    label.text = [NSString stringWithFormat: @"设备+%zd",indexPath.row];
    NewEquipment *item = [self.deviceArr objectAtIndex:indexPath.row];
    label.text = item.name;
    
    UIImageView *selectImg = [cell viewWithTag:2];//selectImage

    if ([self.selectedArr[indexPath.row] isEqualToString:@"1"]) {
        [selectImg setImage:[UIImage imageNamed:@"icon_xuanzhong"]];
    }else{
        [selectImg setImage:[UIImage imageNamed:@"icon_weixuanze"]];
    }

    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.selectedArr[indexPath.row] isEqualToString:@"0"]) {
        [self.selectedArr replaceObjectAtIndex:indexPath.row withObject:@"1"];
    }else{
        [self.selectedArr replaceObjectAtIndex:indexPath.row withObject:@"0"];
    }
    
    [tableView reloadData];
}

- (IBAction)confirmBtnAction:(id)sender {
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    NSArray *dataSceneArr =  [userDefaults arrayForKey:@"Scene"];//Scene
    
    NSMutableArray *SceneArr = [NSMutableArray arrayWithArray:dataSceneArr];//Scene
    NSMutableDictionary *SceneArrDic = [NSMutableDictionary dictionary];//SceneSetDic
    NSMutableArray *sceneSetDeviceArr = [NSMutableArray array];//sceneSetDevice
    
    for (int i = 0; i < self.selectedArr.count; i++) {
        
        if ([self.selectedArr[i] isEqualToString:@"1"]) {
            NewEquipment *tempEquipment = self.deviceArr[i];
            
            NSMutableDictionary *tempDeviceDic = [NSMutableDictionary dictionary];//tempDeviceDic
            [tempDeviceDic setValue:tempEquipment.switchId forKey:@"switchID"];
            [tempDeviceDic setValue:tempEquipment.name forKey:@"deviceName"];
            
            [sceneSetDeviceArr addObject:tempDeviceDic];
        }
    }
    
    [SceneArrDic setObject:self.secenNameTextField.text forKey:@"name"];
    [SceneArrDic setObject:sceneSetDeviceArr forKey:@"sceneSetDevice"];
    
    
    [SceneArr replaceObjectAtIndex:self.sceneIndex-1 withObject:SceneArrDic];

    [userDefaults setObject:SceneArr forKey:@"Scene"];
    
    [userDefaults synchronize];
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)goBackAction:(id)sender {
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark textFieldDelegate
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    if (textField.text.length == 0) {
        textField.text = self.sceneName;
    }
    [textField endEditing:YES];
    return YES;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (self.secenNameTextField.text.length == 0) {
        self.secenNameTextField.text = self.sceneName;
    }
    [self.view endEditing:YES];
}

- (void)dealloc {
    [_SceneSetTableView release];
    [_secenNameTextField release];
    [super dealloc];
}
@end
